package elearning.user.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Random;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import elearning.user.entity.User;
import elearning.user.service.UserService;

/**
 * 
 * @author Administrator
 *
 */
public class UserAction extends ActionSupport implements ModelDriven<User>{


	//注入用户的service
	private  UserService userService;
	//接收验证码
			private String checkcode;	
			public void setCheckcode(String checkcode) {
				this.checkcode = checkcode;
			}
	//模型驱动
	private User user = new User();
		
		public User getModel() {
			
			return user;
		}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public void CheckUserName() throws IOException{
				HttpServletResponse response = ServletActionContext.getResponse();
				PrintWriter out = response.getWriter();
				response.setHeader("Access-Control-Allow-Origin","*");
				boolean f = userService.findByName(user);
				if(f)	{			
				out.print("1");
				}else {
					out.print("0");
				}
 				
				
				
		}
	public void register() throws IOException{
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = response.getWriter();
		response.setHeader("Access-Control-Allow-Origin","*");
		user.setUserDate(new Date().toString());
		
		user.setUserImg(new Random().nextInt(9)+1);
		userService.save(user);
	}
	
	public void active() throws IOException{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		//根据激活码查询用户
				User existUser = userService.findByCode(user.getUserCode());
				if(existUser == null)
				{
					out.print("激活失败：激活码错误！");
				}
				else
				{			
					//激活成功
					//修改用户的状态
					existUser.setUserState(1);
					existUser.setUserCode(null);
					userService.update(existUser);
					out.print("激活成功！");					
				}
				
	}
	
	public void login() throws IOException{	
		HttpServletRequest  request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		User existUser = userService.login(user);
		HttpServletResponse response = ServletActionContext.getResponse();		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
	
		PrintWriter out = response.getWriter();	
		if(existUser == null)
		{
			//登录失败
			out.print("-1");
		
		}else{				
			
			session.setAttribute("existUser",existUser);
		}
	}
	
	public void checklogin() throws IOException{
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = response.getWriter();
		response.setHeader("Access-Control-Allow-Origin","*");
		user = userService.checkUserState(user.getUserName());
		System.out.println(user.getUserState());
		if(user.getUserState()==0){
			out.print("0");
		}else{
			out.print("1");
		}
	}
	public String PersonCenter()
	{
		
		return "success";		
	}
	
	//登录后返回个性推荐数据
	public void personal() throws IOException{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
	
		response.setHeader("Access-Control-Allow-Origin","*");
		System.out.println("**********#####3");
	}
	
	public String quit()
	{
		ServletActionContext.getRequest().getSession().invalidate();
		
		return "quit";
	}
}
